package Guangnan.Wu.clock.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Guangnan.Wu.clock.model.ClockBean;
import Guangnan.Wu.clock.model.DateTimeFormatException;

/**
 * Servlet implementation class ClockServlet
 */
@WebServlet("/clock")
public class ClockServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
	List<String> CallHistory;
    public List<String> getCallHistory() {
		return CallHistory;
	}

	public void setCallHistory(List<String> callHistory) {
		CallHistory = callHistory;
	}

	public ClockServlet() {
        super();
        CallHistory = new ArrayList<String>();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
		rd.forward(request, response);
		return;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		ClockBean clock = new ClockBean();
		String time = null;
		Locale locale = null;
		try {
			String dt =request.getDispatcherType().toString() ;
			if(dt == "FORWARD" )
			{
				CallHistory.clear();
				locale = (Locale)request.getSession().getAttribute("locale");
				time = clock.getCurrentTimeFormatted("full",locale);
			}	
			else if (dt == "REQUEST")
			{
				String format = request.getParameter("format").trim();
				locale = (Locale)request.getSession().getAttribute("locale");
				time = clock.getCurrentTimeFormatted(format,locale);
			}
			CallHistory.add(time);
			HttpSession session = request.getSession();
			session.setAttribute("CallHistory", CallHistory);
			if (locale == Locale.CANADA)
			{
				RequestDispatcher rd = request.getRequestDispatcher("ca_en_clock.jsp");
				rd.forward(request, response);
			}
			else if (locale == Locale.CANADA_FRENCH)
			{
				RequestDispatcher rd = request.getRequestDispatcher("ca_fr_clock.jsp");
				rd.forward(request, response);
			}
			else if (locale == Locale.CHINESE)
			{
				RequestDispatcher rd = request.getRequestDispatcher("cn_zh_clock.jsp");
				rd.forward(request, response);
			}
		} catch (DateTimeFormatException e) {
			System.out.println(e.getStackTrace());
		}
		return;
	}

}
